/*
 * @Author: xie shan
 * @Date: 2023-10-11 11:27:19
 * @LastEditTime: 2023-10-11 14:44:14
 * @LastEditors: xie shan
 * @Description: 时间工具类
 * @FilePath: /grpinbmc-app/src/utils/dateUtils.js
 *
 */

/**
 * @Author: xie shan
 * @description: 获取当前日期
 * @return {*}
 */
export function getDateTime(date, fmt) {
  if (!fmt) {
    fmt = 'yyyy/MM/dd'
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'S+': date.getMilliseconds()
  }

  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}
